package com.nf.health.app.mine.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.CommentAdapter;
import com.nf.health.app.adapter.GridViewAdapter;
import com.nf.health.app.adapter.HealthSchemeAdapter;
import com.nf.health.app.customview.MyGridView;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.models.CommentVo;
import com.nf.health.app.models.MapVo;
import com.nf.health.app.models.SystemSchemeVo;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 系统田佳佳健康详情页面
 * Created by Administrator on 2016/3/23.
 */
public class HealthSchemeSystemActivity extends BaseActivity {


    List<SystemSchemeVo> mlist;


    Map<String, String> map2 = new TreeMap<String, String>();

    List<MapVo> mlist_gridview;

    List<String> list = new ArrayList<>();
    @Bind(R.id.jtjh_lin)
    LinearLayout jtjhLin;


    @Bind(R.id.down_lin)
    LinearLayout downLin;//底部按钮


    @Bind(R.id.add_btn_system)//系统添加按钮
            Button addBtnSystem;


    @Bind(R.id.down_add)
    LinearLayout downAdd;//布局

    private CommentAdapter commentAdapter;//评论适配器
    List<CommentVo> commentVos;

    private HealthSchemeAdapter healthSchemeAdapter;

    String detail, totaldaynum, daynum, name, id, planids, everydaynum, schemeid, schemetempletid;
    @Bind(R.id.hs_title_name)//名称
            TextView hsTitleName;
    @Bind(R.id.hs_tiele_bfb)//百分百
            TextView hsTieleBfb;
    @Bind(R.id.text1_yincang)//添加时不需要的参数
            TextView text1_yincang;
    @Bind(R.id.text2_yincang)//添加时不需要的参数
            TextView text2_yincang;
    @Bind(R.id.hs_tiele_ts)//天数
            TextView hsTieleTs;
    @Bind(R.id.hs_tv_ms)//描述
            TextView hsTvMs;
    @Bind(R.id.ji_listview)//listview
            ListView ji_listview;

    @Bind(R.id.comment_listview)//评论listview
            MyListView comment_listview;




    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();

    int page = 1;
    int pagesize = 20;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_healthscheme);
        ButterKnife.bind(this);


        downAdd.setVisibility(View.VISIBLE);
        ViewUtil.setText(this, R.id.titlebar_title_tv,"添加方案");

        detail = getIntent().getStringExtra("detail");//描述
        totaldaynum = getIntent().getStringExtra("totaldaynum");//共有
        daynum = getIntent().getStringExtra("daynum");//已完成
        name = getIntent().getStringExtra("name");//名称
        planids = getIntent().getStringExtra("planids");//planids
        everydaynum = getIntent().getStringExtra("everydaynum");//everydaynum
        id = getIntent().getStringExtra("id");//id
        schemetempletid = getIntent().getStringExtra("schemetempletid");//健康列表id


        addBtnSystem.setOnClickListener(this);


        hsTvMs.setText("描述" + detail);

        hsTieleTs.setText(totaldaynum + "天");
        text1_yincang.setVisibility(View.INVISIBLE);
        hsTieleBfb.setVisibility(View.INVISIBLE);
        text2_yincang.setVisibility(View.INVISIBLE);
        schemetempletid = getIntent().getStringExtra("id");//健康列表id
        hsTitleName.setText(name);
        provider.requestHealthSchemeSystem(id, "HealthScheme");

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {
            case R.id.add_btn_system://添加健康事件

                provider.requestAddHealthScheme(schemetempletid,"AddHealth");

                break;
        }
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (actionName.equals("HealthScheme")) {


            provider.requestCommentList(schemetempletid, page + "", pagesize + "", "Comment");//请求  用户评论列表

            mlist = (List<SystemSchemeVo>) object;

            jtjhLin.removeAllViews();

            for (int i = 0; i < mlist.size(); i++) {
                View view = View.inflate(this, R.layout.item_health_scheme_item, null);


                TextView chongfu = (TextView) view.findViewById(R.id.chongfu);//重复运动周期

                TextView health_scheme_name = (TextView) view.findViewById(R.id.health_scheme_name);//具体计划名称
                TextView health_scheme_ts = (TextView) view.findViewById(R.id.health_scheme_ts);//具体计划天数
                LinearLayout lin_disan = (LinearLayout) view.findViewById(R.id.lin_disan);//具体计划点击展开
                final MyGridView health_scheme_gridview = (MyGridView) view.findViewById(R.id.health_scheme_gridview);//点击展开隐藏的布局
                final TextView yi_health_scheme_name = (TextView) view.findViewById(R.id.yi_health_scheme_name);//展开后的方案名称
                final TextView text3_yincang = (TextView) view.findViewById(R.id.text3_yincang);//展开后的完成百分之


                final LinearLayout lin_xx = (LinearLayout) view.findViewById(R.id.lin_xx);//方案详情


                health_scheme_name.setText(mlist.get(i).getName());//名称赋值
                yi_health_scheme_name.setText(mlist.get(i).getName());//名称赋值
                lin_disan.setVisibility(View.GONE);
                text3_yincang.setVisibility(View.INVISIBLE);
                lin_xx.setTag(i);//健康方案详情
                health_scheme_ts.setText(totaldaynum + "天");



                GridViewAdapter myAdapter = new GridViewAdapter(mlist_gridview, this);
                health_scheme_gridview.setAdapter(myAdapter);





                lin_xx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos2 = (Integer) v.getTag();
                        Bundle bundle = new Bundle();
                        bundle.putString("id", mlist.get(pos2).getId());
                        ActivityUtils.showActivity(HealthSchemeSystemActivity.this, SystemSchemeDetailActivity.class, bundle);

                    }
                });



                if (mlist.get(i).getType().equals("0")) {
                    chongfu.setText("重复: " + " 每天");
                } else {
                    chongfu.setText("重复 :" + mlist.get(i).getType().replace("1", " 周一").replace("2", " 周二").replace("3", " 周三").replace("4", " 周四").replace("5", " 周五").replace("6", " 周六").replace("7", " 周日"));
                }
                jtjhLin.addView(view);
            }
        } else if (actionName.equals("AddHealth")) {
            showToast("添加方案成功");
            finish();

        } else if (actionName.equals("Comment")) {
            commentVos = (List<CommentVo>) object;
            commentAdapter = new CommentAdapter(commentVos, this);
            comment_listview.setAdapter(commentAdapter);

        }
    }
}
